package com.jlnk.stockdemo.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.jlnk.stockdemo.R;
import com.jlnk.stockdemo.activity.LoginActivity;
import com.jlnk.stockdemo.activity.RegisterActivity;
import com.jlnk.stockdemo.dialog.ChangePasswordDialog;
import com.jlnk.stockdemo.utils.Constants;
import com.jlnk.stockdemo.utils.FileUtils;
import com.jlnk.stockdemo.utils.ImageUtils;
import com.jlnk.stockdemo.utils.SPUtils;

import java.io.File;
import java.io.IOException;

public class MineFragment extends Fragment {
    private ImageView image_avator;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        image_avator = view.findViewById(R.id.image_avator);
        TextView tv_username = view.findViewById(R.id.tv_username);
        Button btn_logout = view.findViewById(R.id.btn_logout);
        Button btn_change_password = view.findViewById(R.id.btn_change_password);
        Button btn_cancel_account = view.findViewById(R.id.btn_cancel_account);
        Button btn_change_avator = view.findViewById(R.id.btn_change_avator);
        if (!SPUtils.getAvatorFilePath().isEmpty()) {
            // 使用文件路径设置ImageView的图片
            Uri imageUri = Uri.fromFile(new File(SPUtils.getAvatorFilePath()));
            image_avator.setImageURI(imageUri);
        }
            tv_username.setText(SPUtils.getUsername());
        btn_change_avator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openImagePicker();
            }
        });
        btn_change_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChangePasswordDialog dialog = new ChangePasswordDialog(getActivity());
                dialog.show();
            }
        });
        btn_cancel_account.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SPUtils.clear();
                startActivity(new Intent(getActivity(), RegisterActivity.class));
                getActivity().finish();
            }
        });
        btn_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), LoginActivity.class));
                getActivity().finish();
            }
        });
    }

    private void openImagePicker() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), Constants.MINE_FRAGMENT_REQUEST_CODE);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null || data.getData() == null) {
            return;
        }
        Uri selectedImageUri = data.getData();
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImageUri);
            image_avator.setImageBitmap(bitmap);
            // 将Bitmap转换为JPG文件
            String outputPath = getActivity().getExternalFilesDir(null) + "/avatar.jpg"; // 使用外部存储的私有目录
            File jpgFile = ImageUtils.convertBitmapToJPG(bitmap, outputPath, 90); // 假设我们设置质量为90
            // 保存JPG文件到应用的私有目录
            String savedFilePath = FileUtils.saveJPGFile(getActivity(), jpgFile, "avatar.jpg");
            SPUtils.saveAvatorFilePath(savedFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
